<template>
  <div class="container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 10px">
      <el-row>
        <el-button type="primary" size="small" @click="showDialog = true"
          >添加角色</el-button
        >
      </el-row>
      <el-table border :data="roleList" style="margin-top: 20px">
        <el-table-column type="expand">
          <template #default="{ row }">
            <el-alert
              v-if="row.children.length === 0"
              title="暂无权限"
              :closable="false"
            ></el-alert>
            <el-row
              v-for="(item1, i1) in row.children"
              :key="item1.id"
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'middle']"
            >
              <el-col :span="5">
                <el-tag closable @close="delRight(row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                  :class="[i2 !== 0 ? 'bdtop' : '', 'middle']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="delRight(row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      type="warning"
                      closable
                      @close="delRight(row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="editRoles(row)"
              >编辑</el-button
            >
            <el-button size="small" type="danger" @click="delRoles(row.id)"
              >删除</el-button
            >
            <el-button size="small" type="warning" @click="allot(row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <AllotRights
      :showAllot.sync="showAllot"
      :currentId="currentId"
      ref="AllotRights"
    />
    <!-- 添加角色对话框 -->
    <addAndEditRoles :showDialog.sync="showDialog" ref="dialog" />
  </div>
</template>

<script>
import { roles, delRoles } from '@/api/roles'
import { delRightsById } from '@/api/rights'
import AllotRights from './allotRights.vue'
import addAndEditRoles from './addAndEditRoles.vue'
export default {
  components: { AllotRights, addAndEditRoles },
  data () {
    return {
      roleList: [],
      showAllot: false,
      currentId: 0, // 当前分配的角色ID
      showDialog: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      this.roleList = await roles()
    },
    async delRight (row, rightId) {
      try {
        const res = await delRightsById(row.id, rightId)
        row.children = res
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    async allot (row) {
      this.currentId = row.id
      this.$refs.AllotRights.getRightsKeys(row.children)
      this.showAllot = true
    },
    async delRoles (id) {
      try {
        await this.$confirm('此操作将永久删除该角色,是否继续', {
          type: 'warning'
        })
        await delRoles(id)
        this.$message.success('删除成功')
        this.getList()
      } catch (err) {
        console.log(err)
      }
    },
    editRoles (row) {
      this.$refs.dialog.getDetail(row)
      this.showDialog = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.middle {
  display: flex;
  align-items: center;
  padding: 1px 0;
}
::v-deep .el-table__expanded-cell[class*="cell"] {
  padding: 10px 50px;
}
</style>
